package com.springboot.controller.web;
import com.springboot.model.*;
import com.springboot.service.DoctorCancelAppointService;
import com.springboot.service.PatientMyInformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 本控制台用于医生取消预约
 */
@Controller
public class DoctorCancelOrderController {
    @Autowired
    DoctorCancelAppointService doctorCancelAppointService;

    @Autowired
    PatientMyInformService patientMyInformService;
    /**
     * 医生取消预约
     */
    @CrossOrigin
    @RequestMapping(value = "/webdoctorCancelAppoint")
    public @ResponseBody
    String webDoctorCancelAppoint(@RequestParam("bookId") Integer bookId,
                                  HttpServletRequest request) {
        System.out.println("医生取消预约");
        try {

            OrderForm orderForm = patientMyInformService.findOrderFormByBookId(bookId);
            Integer patientId = orderForm.getPatientId();
            Integer doctorId = orderForm.getDoctorId();
            java.sql.Date date = orderForm.getBootDate();
            String orderTime = orderForm.getBootTime();
            //医生取消预约之后就将这条预约记录删除
            doctorCancelAppointService.deleteOrderMessage(bookId);
            MessageInform messageInform=new MessageInform();
            messageInform.setPatientId(patientId);
            messageInform.setDoctorId(doctorId);
            messageInform.setBookDate(date);
            messageInform.setBookTime(orderTime);
            messageInform.setMessageState1("未读");
            messageInform.setMessageState2("未读");
//----------------修改：2020-12-25  张-------------~~~~~~~~~~~~~~~~~~~~~~----------
            SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime=dateTime.format(new Date());
            java.sql.Date sqlDate = new java.sql.Date(dateTime.parse(currentTime).getTime());
            messageInform.setMessageDatetime(sqlDate);
            messageInform.setMessageMark(2);
            //将医生取消患者预约存储在消息表中
            doctorCancelAppointService.insertIntoMessageList(messageInform);
            MessageInform message=doctorCancelAppointService.findMessageByRecord(messageInform);
            int messageId=message.getMessageId();
            System.out.println("messageId: "+messageId);
            ArrayList<AssistantListFormKey> assistantListFormKeys=doctorCancelAppointService.findAssistantByDoctorId(doctorId);
            int []assistantId=new int[assistantListFormKeys.size()];
            for(int i=0;i<assistantListFormKeys.size();i++)
            {
                AssistantListFormKey assistantListFormKey=assistantListFormKeys.get(i);
                assistantId[i]=assistantListFormKey.getAssistantId();
            }
            //找到负责该医生的所有助理编号
            for(int i=0;i<assistantListFormKeys.size();i++)
            {
                System.out.println("assistantId: "+assistantId[i]);
            }
            //向每个负责助理信息表中插入消息记录
            for(int i=0;i<assistantListFormKeys.size();i++)
            {
                AssistantMessageInform assistantMessageInform=new AssistantMessageInform();
                assistantMessageInform.setMessageId(messageId);
                assistantMessageInform.setAssistantId(assistantId[i]);
                assistantMessageInform.setMessageState3("未读");
                doctorCancelAppointService.insertIntoAssistantMessageList(assistantMessageInform);
            }
            return "True";
        } catch (Exception e) {
            return "False";
        }
    }
}
